package com.taotao.search.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.taotao.common.pojo.SearchResult;
import com.taotao.search.service.SearchItem;

//商品搜索
@Controller
public class SearchController {

	@Autowired
	private SearchItem searchService;
	
	@Value("${ITEM_ROWS}")
	private Integer ITEM_ROWS;
	
	@RequestMapping("/search")
	public String searchItem(@RequestParam("q")String queryString,
			@RequestParam(defaultValue="1")Integer page,Model model) throws Exception{
		//解决get乱码问题
		queryString=new String(queryString.getBytes("iso8859-1"), "utf-8");
		//调用服务搜索商品信息
		SearchResult searchResult = searchService.search(queryString, page, ITEM_ROWS);
		model.addAttribute("query", queryString);
		model.addAttribute("totalPages", searchResult.getPageCount());
		model.addAttribute("itemList", searchResult.getItemList());
		model.addAttribute("page", page);
		//返回逻辑视图
		model.addAttribute("test", page);
		model.addAttribute("test", page);
		return "search";
	}
}
